<?php

namespace App\Console\Commands;

use App\Models\MallOrder;
use App\Services\MallOrderService;
use Illuminate\Console\Command;

class CloseOrder extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'close_order';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $yesterday = date("Y-m-d H:i:s",strtotime("-3 day"));
//        $yesterday = date("Y-m-d H:i:s",time() - 2 * 60);
        MallOrder::where('created_at', '<', $yesterday)->where('status', MallOrder::STATUS_WAIT_PAY)->chunk(50, function ($list) {
            foreach ($list as $key => $val) {
                $input = [
                    'order_id' => $val->id,
                    'close_type' => MallOrder::CLOSE_TYPE_SYSTEM
                ];
                MallOrderService::handle($input, 'cancel');
            }
        });
    }
}
